<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="档案号" prop="archivesNo">
        <el-input
          v-model="queryParams.archivesNo"
          placeholder="请输入档案号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
	  <el-form-item label="主题" prop="subject">
	    <el-input
	      v-model="queryParams.subject"
	      placeholder="请输入主题"
	      clearable
	      @keyup.enter.native="handleQuery"
	    />
	  </el-form-item>
      <el-form-item label="问题类型" prop="qaTypeId">
		  <el-select v-model="queryParams.qaTypeName" @change="queryTypeChange">
		    <el-option v-for="(item,idx) in qaTypeList" 
		    :label="item.typeName" 
		    :value="item.qaTypeId"
		    :key="idx"
		    ></el-option>
		  </el-select>
      </el-form-item>
      
      <el-form-item label="结案状态" prop="state">
		<el-select v-model="queryParams.state">
		   <el-option value="0" label="未结案">未结案</el-option>
		   <el-option value="1" label="已结案">已结案</el-option>
		</el-select>
		</el-form-item>
		<el-form-item label="起始日期" prop="startDate">
		  <el-date-picker clearable
		    v-model="startDate"
		    type="date"
		    value-format="yyyy-MM-dd"
		    placeholder="请选择起始日期">
		  </el-date-picker>
		</el-form-item>
		<el-form-item label="截止日期" prop="endDate">
		  <el-date-picker clearable
		    v-model="endDate"
		    type="date"
		    value-format="yyyy-MM-dd"
		    placeholder="请选择截止日期">
		  </el-date-picker>
		</el-form-item>
      
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
		
		<el-button icon="el-icon-refresh" size="mini" @click="close()">返回</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="archivesList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="archivesGroupId" width="80"/>
      <el-table-column label="档案号" align="center" prop="archivesNo" />
      <el-table-column label="主题" align="center" prop="subject" />
      <el-table-column label="问题类型" align="center" prop="typeName" />
	  <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="咨询师" align="teaFullName" prop="state" />		  
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
	<el-button type="primary" @click="submitForm">生成报表</el-button>
  </div>
</template>

<script>
import { listArchives } from "@/api/consult/archives-group";
import { addReport } from "@/api/consult/report";
import { selectQaType } from "@/api/consult/common";

export default {
  name: "addConsultReport2",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        archivesNo: null,
        qaTypeId: null,
		qaTypeName: '',
        fullName: null,
        state: null,
		params: {
			
		}
      },
      // 表单参数
      form: {
		  reportId: null,
		  reportName: null,
		  consultType: 2,
		  ids: []
	  },
	  archivesList: [],
      qaTypeList: [],
	  startDate: '',
	  endDate: '',
    };
  },
  created() {
	const title = this.$route.query && this.$route.query.title;
	this.form.reportName = title;
	this.getQaTypeList();
    this.getList();
  },
  methods: {
	  getQaTypeList() {
	    selectQaType().then(response => {
	      this.qaTypeList = response.data;
	    });
	  },
	  queryTypeChange(val) {
	  		  this.queryParams.qaTypeId = val;
	  },
	  /** 搜索按钮操作 */
	  handleQuery() {
	    this.queryParams.pageNum = 1;
	    this.getList();
	  },
	  // 多选框选中数据
	  handleSelectionChange(selection) {
	    this.ids = selection.map(item => item.archivesGroupId)
	    this.single = selection.length!==1
	    this.multiple = !selection.length
	  },
	 /** 查询咨询个人档案列表 */
	 getList() {
	   this.loading = true;
	   this.queryParams.params['startDate'] = this.startDate;
	   this.queryParams.params['endDate'] = this.endDate;
	   listArchives(this.queryParams).then(response => {
	     this.archivesList = response.rows;
	     this.total = response.total;
	     this.loading = false;
	   });
	 },
	/** 提交按钮 */
    submitForm() {
		if(this.ids.length == 0) {
			this.$modal.msgError("请选择档案");
			return false;
		}
		this.form.ids = this.ids;
	  addReport(this.form).then(response => {
		this.$modal.msgSuccess("新增成功");
		this.close();
	  });
    },
	close() {
		const obj = { path: "/consult/report" };
		this.$tab.closeOpenPage(obj);
	}
  }
};
</script>
